package cn.iocoder.yudao.module.wrdk.service.land;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wrdk.controller.admin.land.vo.*;
import cn.iocoder.yudao.module.wrdk.dal.dataobject.land.wrdklandDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.wrdk.convert.land.wrdklandConvert;
import cn.iocoder.yudao.module.wrdk.dal.mysql.land.wrdklandMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wrdk.enums.ErrorCodeConstants.*;

/**
 * 污染地块安全利用率调查 Service 实现类
 *
 * @author admin
 */
@Service
@Slf4j
@Validated
public class wrdklandServiceImpl implements wrdklandService {

    @Resource
    private wrdklandMapper wrdklandMapper;

    @Override
    public Long createwrdkland(wrdklandCreateReqVO createReqVO) {
        // 插入
        wrdklandDO wrdkland = wrdklandConvert.INSTANCE.convert(createReqVO);
        wrdklandMapper.insert(wrdkland);
        // 返回
        return wrdkland.getId();
    }

    @Override
    public void updatewrdkland(wrdklandUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatewrdklandExists(updateReqVO.getId());
        // 更新
        wrdklandDO updateObj = wrdklandConvert.INSTANCE.convert(updateReqVO);
        wrdklandMapper.updateById(updateObj);
    }

    @Override
    public void deletewrdkland(Long id) {
        // 校验存在
        this.validatewrdklandExists(id);
        // 删除
        wrdklandMapper.deleteById(id);
    }

    private void validatewrdklandExists(Long id) {
        if (wrdklandMapper.selectById(id) == null) {
            throw exception(WRDKLAND_NOT_EXISTS);
        }
    }

    @Override
    public wrdklandDO getwrdkland(Long id) {
        return wrdklandMapper.selectById(id);
    }

    @Override
    public List<wrdklandDO> getwrdklandList(Collection<Long> ids) {
        return wrdklandMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<wrdklandDO> getwrdklandPage(wrdklandPageReqVO pageReqVO) {
        return wrdklandMapper.selectPage(pageReqVO);
    }

    @Override
    public List<wrdklandDO> getwrdklandList(wrdklandExportReqVO exportReqVO) {
        return wrdklandMapper.selectList(exportReqVO);
    }

    @Override
    public void deleteSelectLand(List<Long> ids) {
        for (Long id:ids) {
            // 校验存在
            this.validatewrdklandExists(id);
            // 删除
            wrdklandMapper.deleteById(id);
        }
    }

}
